********************************************************************************
********************************************************************************
******** Issues vs. Affect: How Do Elite and Mass Polarization Compare? ********
********************************************************************************
********************************************************************************

****
** Import data and generate
** new variables
****

use "/Users/adamenders/Dropbox/Issues vs. Affect/Data and Code/Mass Data.dta"
append using "/Users/adamenders/Dropbox/Issues vs. Affect/Data and Code/Elite Data.dta"
append using "/Users/adamenders/Dropbox/Issues vs. Affect/Data and Code/anes2016ts.dta"

drop if sample == 2 & publicpos == 0
drop if sample == 2 & publicpos == .

gen samplealt = "Masses" if sample == 1
replace samplealt = "Elites" if sample == 2
	
gen yearalt = 0 if year == 1972
replace yearalt = 1 if year == 1980
replace yearalt = 2 if year == 1984	
replace yearalt = 3 if year == 1988
replace yearalt = 4 if year == 1992
replace yearalt = 5 if year == 2000
replace yearalt = 6 if year == 2004
replace yearalt = 7 if year == 2008
replace yearalt = 8 if year == 2012
replace yearalt = 9 if year == 2016
	
* Group by PID	
gen pid3 = 1 if pid > 0 & pid < . & sample == 1
replace pid3 = 2 if pid == 0 & sample == 1
replace pid3 = 3 if pid < 0  & sample == 1

gen group = "Republican/ANES" if sample == 1 & rep == 1
replace group = "Democrat/ANES" if sample == 1 & rep == 0
replace group = "Republican/CDS" if sample == 2 & rep == 1	
replace group = "Democrat/CDS" if sample == 2 & rep == 0
replace group = "Indepdendent/ANES" if sample == 1 & pid == 0

gen repalt = "Republican" if rep == 1
replace repalt = "Democrat" if rep == 0


* Group by Ideology
gen ideo3 = 1 if ideo > 0 & ideo < .
replace ideo3 = 2 if ideo == 0
replace ideo3 = 3 if ideo < 0

gen group2 = "Conservative/ANES" if sample == 1 & ideo3 == 1
replace group2 = "Moderate/ANES" if sample == 1 & ideo3 == 2
replace group2 = "Liberal/ANES" if sample == 1 & ideo3 == 3
replace group2 = "Conservative/CDS" if sample == 2 & ideo3 == 1	
replace group2 = "Moderate/CDS" if sample == 2 & ideo3 == 2
replace group2 = "Liberal/CDS" if sample == 2 & ideo3 == 3

gen conserv = 1 if ideo > 0 & ideo < .
replace conserv = 0 if ideo < 0

********************************************************************************

****
** Restrict analyses to presidential
** election years starting in 1972
****

keep if year == 1972 | year == 1976 | year == 1980 | year == 1984 | ///
	year == 1988 | year == 1992 | year == 1996 | year == 2000 | ///
	year == 2004 | year == 2008 | year == 2012 | year == 2016

****
** Generate standardized feeling thermometer
** and issue attitudes measures for 
** masses and elites
****

foreach v of var conservtherm liberaltherm dempartytherm ///
	reppartytherm demcandtherm repcandtherm selfdefense ///
	selfaid selfservice selfabort selfinsure{ 
	sum `v' if sample == 1 
	gen `v'ANES = (`v' - r(mean))/r(sd)
	replace `v'ANES = . if sample == 2
}

foreach v of var conservtherm liberaltherm dempartytherm ///
	reppartytherm demcandtherm repcandtherm selfdefense ///
	selfaid selfservice selfabort selfinsure{ 
	sum `v' if sample == 2
	gen `v'CDS = (`v' - r(mean))/r(sd)
	replace `v'CDS = . if sample == 1
}

egen conservtherm2 = rowfirst(conservthermANES conservthermCDS)
egen liberaltherm2 = rowfirst(liberalthermANES liberalthermCDS)
egen dempartytherm2 = rowfirst(dempartythermANES dempartythermCDS)
egen reppartytherm2 = rowfirst(reppartythermANES reppartythermCDS)
egen demcandtherm2 = rowfirst(demcandthermANES demcandthermCDS)
egen repcandtherm2 = rowfirst(repcandthermANES repcandthermCDS)

egen selfdefense2 = rowfirst(selfdefenseANES selfdefenseCDS)
egen selfaid2 = rowfirst(selfaidANES selfaidCDS)
egen selfservice2 = rowfirst(selfserviceANES selfserviceCDS)
egen selfabort2 = rowfirst(selfabortANES selfabortCDS)
egen selfinsure2 = rowfirst(selfinsureANES selfinsureCDS)

* Create summary index
alpha conservtherm2 liberaltherm2 dempartytherm2 reppartytherm2 ///
	demcandtherm2 repcandtherm2, gen(feelingindex)

alpha selfdefense2 selfaid2 selfservice2 selfabort2 ///
	selfinsure2, gen(issueindex)	
	
****
** Generate average partisan differences
** in feeling thermometers and issue
** attitudes, by sample
****

* Feeling thermometers
bysort year: ttest liberaltherm2 if sample == 1, by(rep)
bysort year: ttest liberaltherm2 if sample == 2, by(rep)

bysort year: ttest conservtherm2 if sample == 1, by(rep)
bysort year: ttest conservtherm2 if sample == 2, by(rep)

bysort year: ttest dempartytherm2 if sample == 1, by(rep)
bysort year: ttest dempartytherm2 if sample == 2, by(rep)

bysort year: ttest reppartytherm2 if sample == 1, by(rep)
bysort year: ttest reppartytherm2 if sample == 2, by(rep)

bysort year: ttest demcandtherm2 if sample == 1, by(rep)
bysort year: ttest demcandtherm2 if sample == 2, by(rep)

bysort year: ttest repcandtherm2 if sample == 1, by(rep)
bysort year: ttest repcandtherm2 if sample == 2, by(rep)

* Issues
bysort year: ttest selfservice2 if sample == 1, by(rep)
bysort year: ttest selfdefense2 if sample == 1, by(rep)
bysort year: ttest selfaid2 if sample == 1, by(rep)
bysort year: ttest selfabort2 if sample == 1, by(rep)
bysort year: ttest selfinsure2 if sample == 1, by(rep)

bysort year: ttest selfservice2 if sample == 2, by(rep)
bysort year: ttest selfdefense2 if sample == 2, by(rep)
bysort year: ttest selfaid2 if sample == 2, by(rep)
bysort year: ttest selfabort2 if sample == 2, by(rep)
bysort year: ttest selfinsure2 if sample == 2, by(rep)
	
****
** Estimate ideological dimension
** using issue attitudes and feeling
** thermometers for masses and elites
****

gen r_liberaltherm = round(liberaltherm, 10)
gen r_conservtherm = round(conservtherm, 10)
gen r_dempartytherm = round(dempartytherm, 10)
gen r_reppartytherm = round(reppartytherm, 10)
gen r_repcandtherm = round(repcandtherm, 10)
gen r_demcandtherm = round(demcandtherm, 10)

irt grm r_conservtherm r_liberaltherm r_dempartytherm r_reppartytherm /// 
	r_demcandtherm r_repcandtherm 
predict feelingscore, latent

irt grm selfdefense selfaid selfservice selfinsure selfabort
predict issuescore, latent

****
** Mean partisan differences in ideal 
** point estimates by political stratum
****

bysort year: ttest issuescore if sample == 1, by(rep)
bysort year: ttest issuescore if sample == 2, by(rep)

bysort year: ttest feelingscore if sample == 1, by(rep)
bysort year: ttest feelingscore if sample == 2, by(rep)

****
** Standard deviations of ideal 
** point estimates by political stratum
****

bysort year: sum issuescore if sample == 1
bysort year: sum issuescore if sample == 2

bysort year: sum feelingscore if sample == 1
bysort year: sum feelingscore if sample == 2

****
** Quantile regressions to test equality 
** of party medians by political stratum
****

by year: qreg issuescore rep if sample == 1

qreg issuescore rep if sample == 2 & year == 1980
qreg issuescore rep if sample == 2 & year == 1984
qreg issuescore rep if sample == 2 & year == 1988
qreg issuescore rep if sample == 2 & year == 1992
qreg issuescore rep if sample == 2 & year == 2000
qreg issuescore rep if sample == 2 & year == 2004

bysort year: qreg feelingscore rep if sample == 1
bysort year: qreg feelingscore rep if sample == 2

****
** Party medians in most recent year
** with complete data
****

* Thermometers
sum feelingscore if sample == 1 & year == 2004 & rep == 1, detail
sum feelingscore if sample == 1 & year == 2004 & rep == 0, detail
sum feelingscore if sample == 2 & year == 2004 & rep == 1, detail
sum feelingscore if sample == 2 & year == 2004 & rep == 0, detail

* Issues
sum issuescore if sample == 1 & year == 2004 & rep == 1, detail
sum issuescore if sample == 1 & year == 2004 & rep == 0, detail
sum issuescore if sample == 2 & year == 2004 & rep == 1, detail
sum issuescore if sample == 2 & year == 2004 & rep == 0, detail

********************************************************************************

****
** Appendix Analyses
****

****
** Mean differences by standardized index
****

bysort year: ttest issueindex if sample == 1, by(rep)
bysort year: ttest issueindex if sample == 2, by(rep)

bysort year: ttest feelingindex if sample == 1, by(rep)
bysort year: ttest feelingindex if sample == 2, by(rep)

****
** Standard deviations by standardized index
****

bysort year: sum issueindex if sample == 1
bysort year: sum issueindex if sample == 2

bysort year: sum feelingindex if sample == 1
bysort year: sum feelingindex if sample == 2

****
** Quantile regressions to test equality 
** of party medians by political stratum
****

by year: qreg issueindex rep if sample == 1

qreg issueindex rep if sample == 2 & year == 1980
qreg issueindex rep if sample == 2 & year == 1984
qreg issueindex rep if sample == 2 & year == 1988
qreg issueindex rep if sample == 2 & year == 1992
qreg issueindex rep if sample == 2 & year == 2000
qreg issueindex rep if sample == 2 & year == 2004

bysort year: qreg feelingindex rep if sample == 1
bysort year: qreg feelingindex rep if sample == 2

****
** Estimate ideological dimensions
** spearate for masses and elites to
** check joint scaling assumptions per
** Jessee (2016)
****

irt grm r_conservtherm r_liberaltherm r_dempartytherm r_reppartytherm /// 
	r_demcandtherm r_repcandtherm if sample == 1 & year < 2008
predict feelingsANES, latent
replace feelingsANES = . if sample == 2
replace feelingsANES = feelingsANES * -1

irt grm r_conservtherm r_liberaltherm r_dempartytherm r_reppartytherm /// 
	r_demcandtherm r_repcandtherm if sample == 2 
predict feelingsCDS, latent
replace feelingsCDS = . if sample == 1
replace feelingsCDS = feelingsCDS * -1

irt grm selfdefense selfaid selfservice selfinsure selfabort if sample == 1 ///
	& year == 1972 | year == 1980 | year == 1984 | year == 1988 | ///
	year == 1992 | year == 2000 | year == 2004
predict issuesANES, latent
replace issuesANES = . if sample == 2

irt grm selfdefense selfaid selfservice selfabort selfinsure if sample == 2 ///
	& year == 1972 | year == 1980 | year == 1984 | year == 1988 | ///
	year == 1992 | year == 2000 | year == 2004
predict issuesCDS, latent
replace issuesCDS = . if sample == 1

corr feelingscore feelingsANES if sample == 1
corr feelingscore feelingsCDS if sample == 2

corr issuescore issuesANES if sample == 1
corr issuescore issuesCDS if sample == 2


saveold "Combined, Short.dta", version(12) replace

